//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Graphics/Resources/BitmapFont.h"
#include "Graphics/GLshell/FrameBuffer.h"
#include "Graphics/Loaders/VectorFontManager.h"
#include "Graphics/Resources/VectorFont.h"
#include "Shared/Resources/ResourceFileInfo.h"

namespace Engine
{
namespace Graphics
{


	//
	// Font Loader
	//

	struct FontLoader
	{
		friend struct VectorFontManager;

	// types
	public:
		typedef array< uvec3 >	char_intervals_t;		// first char, first wchar, count
		
	private:
		enum {
			MARGIN_X	= VectorFontManager::_MARGIN_X,
			MARGIN_Y	= VectorFontManager::_MARGIN_Y,
		};

		typedef	struct _FT_Face_ *		FT_Face_t;
		typedef struct _FT_Library_ *	FT_Library_t;

		typedef bool (FontLoader::* LoaderFunc_t) (ResourcePtr &resource, FT_Face_t face, const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess);


	// variables
	private:
		FT_Library_t		_ftLibrary;
		FrameBufferPtr		_fb;
		binary_array_t		_glyphBuffer;

		// bitmap font
		char_intervals_t	_charCache;
		uint				_numChars;

		VectorFontManager	_vecFontMngr;


	// methods
	private:
		static bool _LoadFontFT (ResourcePtr &resource, uni_c_string filename, void *parameter,
								 const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess,
								 LoaderFunc_t loadFunc);
		
		static bool _LoadFontFT (ResourcePtr &resource, const RFilePtr &file, void *parameter,
								 const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess,
								 LoaderFunc_t loadFunc);
		
		static bool _LoadVectorFont (ResourcePtr &resource, const RFilePtr &file, void *parameter,
									 const VectorFont::VectorFontLoadParams &loadParams, const EngineSubSystemsRef ess);

		static bool _AddGlyph (BitmapFontPtr &font, FT_Face_t face, binary_array_t &buf,
								uvec2 &curPos, uvec2 &maxSize, uint16 symb, uint8 code);

		FrameBuffer &	_GetFB (const EngineSubSystemsRef ess);

		bool _LoadBitmapFont (ResourcePtr &resource, FT_Face_t face, const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess);
		
		bool _LoadGlyph (FT_Face_t face, uint8 symbol, uint height, const uvec2 &texDim,
						 OUT VectorFont::VecGlyph &glyph, OUT uvec2 &dim, OUT uni_binbuffer_t &buf);


	public:
		FontLoader ();
		~FontLoader ();

		void SetCharCache (const char_intervals_t &charCache);

		void Destroy ();

		void Register (ResourceManager *rm);
		void Unregister (ResourceManager *rm);

		static bool LoadBitmapFontFromFName (ResourcePtr &resource, uni_c_string filename, void *parameter,
											 const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess);

		static bool LoadBitmapFontFromFile (ResourcePtr &resource, const RFilePtr &file, void *parameter,
											const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess);

		static bool LoadVectorFontFromFile (ResourcePtr &resource, const RFilePtr &file, void *parameter,
											const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess);
		
		// Vector Font Manager
		bool CreateLevel (Ptr< VectorFont > font, uint height);
		bool DestroyLevel (Ptr< VectorFont > font, uint height);
		bool DestroyFontLevels (Ptr< VectorFont > font);

		bool LoadGlyph (Ptr< VectorFont > font, uint8 symbol, uint height, OUT VectorFont::VecGlyph &glyph);
	};


}	// Graphics
}	// Engine